//
// Created by sjjmw on 3/24/2022.
//
#include "iostream"
#include "vector"
#include "list"
#include "algorithm"
#include "string"
#include "unordered_map"
#include "cmath"
#include<iomanip>
#include "string"
#include "map"
#include "list"
struct Stu{
//    int id;
    std::string name;
    int grade;
//    std::string sex;
};
bool cmp(std::pair<std::string,Stu>&p1,
        std::pair<std::string,Stu>&p2) {
    if(p1.second.grade>p2.second.grade){
        return true;
    }else if(p1.second.grade<p2.second.grade){
        return false;
    }else{
        return p1.first=="DaDa";
    }
}
struct Sol{
//    std::unordered_map<int,Stu> id_2_stu;
    std::unordered_map<std::string,Stu> name_2_info;
//    std::vector<Stu*> vec;
    void handle(int n){
        for(int i=0;i<n;i++){
            std::string name;
            std::cin>>name;
            name_2_info[name]={name,0};
//            vec.push_back(&name_2_info[name]);
        }
        int ks_cnt;
        std::cin>>ks_cnt;
        for(int i=0;i<ks_cnt;i++){
            for(int j=0;j<n;j++){
                int score;std::cin>>score;
                std::string name;std::cin>>name;
                name_2_info[name].grade+=score;
            }
            int rank=1;
            int dada_grade=name_2_info["DaDa"].grade;
            for(auto&pair:name_2_info){
                if(pair.second.grade>dada_grade){
                    rank++;
                }
//                rank++;
            }
            printf("%d\n",rank);
        }
    }
};
int main(){
    int a,b,c;
    while(std::cin>>a){
        Sol().handle(a);
    }

    return 0;
}